﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanserClient;
using System.Threading;

namespace Controlador_TB1
{
   
    
    public partial class Form1 : Form
    {
        QuanserClient.QuanserClient quanserClient;
        QuanserServer quanserServer;
        double valor = 0;

        Thread oThread;

        Boolean ondaQuadrada = false;
        Boolean ondaSenoidal = false;

        List<Classes.CB_Item> Portas = new List<Classes.CB_Item>();
        //Classes.CB_Item[] Portas = new Classes.CB_Item();

        /*******************************************************************/

        public Form1()
        {
            InitializeComponent();

            quanserServer = new QuanserServer(20081);
            quanserClient = new QuanserClient.QuanserClient(quanserServer);

            for (int x = 0; x < 8; x++)
            {
                Classes.CB_Item novo = new Classes.CB_Item();
                novo.texto = "Porta" + x.ToString();
                novo.valor = x;

                Portas.Add(novo);
            }
            CB_Portas.DisplayMember = "texto";
            CB_Portas.ValueMember = "valor";
            CB_Portas.DataSource = Portas;

        }

        /*******************************************************************/

        private void button1_Click(object sender, EventArgs e)
        {
            string x = "Porta " + CB_Portas_Leitura.SelectedValue.ToString();

            GV_Valores.Rows.Add(new object[] { x, 0 });
        }

        /*******************************************************************/

        private void button2_Click(object sender, EventArgs e)
        {
            Portas.RemoveAt(Convert.ToInt32(CB_Portas.SelectedValue));
            //.Items.RemoveAt(Convert.ToInt32(CB_Portas.SelectedValue));

            CB_Portas_Leitura.DisplayMember = "texto";
            CB_Portas_Leitura.ValueMember = "valor";
            CB_Portas_Leitura.DataSource = Portas;

            CB_Portas.Enabled = false;
            button2.Enabled = false;
        }

        /*******************************************************************/

        private void button3_Click(object sender, EventArgs e)
        {
            //foreach (DataGridViewRow linha in GV_Valores.Rows)
            //{
            //    linha.Cells[1].Value = "poipoi";
            //}

            



            try
            {
                
                //double _read = quanserClient.read(1);
                //Console.WriteLine("Leitura Canal 0 : " + _read);

                //button3.Text = _read.ToString();
                

                //Console.WriteLine("Gravando 1.2 volts no canal 0...");
                //quanserClient.write(0, Convert.ToDouble(textBox1.Text)/5);
                //quanserClient.write(0, 3.0);
                
            }
            catch (QuanserClientException ex)
            {
                Console.WriteLine(ex.Message);

            }

        }

        /*******************************************************************/

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            valor = Convert.ToDouble( trackBar1.Value) / 10;
        }

        /*******************************************************************/

        private void Quadrada()
        {
            Boolean alto = true;

            ondaQuadrada = true;

            while (ondaQuadrada)
            {
                quanserClient.Write(0, (alto ? valor : 0));

                alto = !alto;

                Thread.Sleep(1500);
            }
        }

        /*******************************************************************/

        private void Senoidal()
        {
            double T = 1;
            double s = 0;

            double valor_final = 0;

            ondaSenoidal = true;

            while (ondaSenoidal)
            {
                valor_final = 0.5 + 0.5 * Math.Sin(((2* Math.PI)*s)/T);

                s = s + 0.1;
                if(s > 1)
                s = 0;
                
                quanserClient.Write(0, (valor_final));

                Thread.Sleep(100);
            }
        }

        /*******************************************************************/

        private void button4_Click(object sender, EventArgs e)
        {
            Cancelar_thread();

            Thread oThread = new Thread(new ThreadStart(Quadrada));

            // Start the thread
            oThread.Start();

        }

        /*******************************************************************/

        private void button5_Click(object sender, EventArgs e)
        {
            Cancelar_thread();
        }

        /*******************************************************************/

        private void Onda_Senoidal_Click(object sender, EventArgs e)
        {
            Cancelar_thread();
            oThread = new Thread(new ThreadStart(Senoidal));

            // Start the thread
            oThread.Start();
        }

        /*******************************************************************/

        private void Cancelar_thread()
        {
            try
            {
                ondaQuadrada = false;
                ondaSenoidal = false;
                oThread.Abort();
            }
            catch { }
        }

        /*******************************************************************/
    }
}
